package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

    /**
     * 扣减账户余额
     * /api/account/userAccount/checkAndDeduct
     */
    @PostMapping("/userAccount/checkAndDeduct")
    public Result checkAndDeduct(@RequestBody AccountLockVo accountLockVo){
        userAccountService.checkAndDeduct(accountLockVo);
        return Result.ok();
    }

    /**
     * 获取账户可用余额
     * /api/account/userAccount/getAvailableAmount
     * @return
     */
    @GetMapping("/userAccount/getAvailableAmount")
    @GuiguLogin
    public Result<BigDecimal> getAvailableAmount(){
        Long userId = AuthContextHolder.getUserId();
        BigDecimal bigDecimal = userAccountService.getAvailableAmount(userId);
        return Result.ok(bigDecimal);
    }
}

